﻿using Microsoft.Extensions.Logging;
using System;

namespace XYAC.Framework.MonitorTireMaterialManual.Extensions
{
    public class AppConfig
    {

        /// <summary>
        /// 本地数据报表数据库/数据缓存库
        /// </summary>
        public string LocalReportConnectionStringTemplate { get; set; }
        /// <summary>
        /// Database数据库地址
        /// </summary>
        public string AccessCAreaBaseConnectionString { get; set; }
        /// <summary>
        /// 自定义日志的历史数据库
        /// </summary>
        public string AccessCAreaHisConnectionString { get; set; }
        /// <summary>
        /// Oracle数据库
        /// </summary>
        public String OracleMesConnectionString { get; set; }
        /// <summary>
        /// 本地数据库连接字符串
        /// </summary>
        public string LocalDataConnectionString { get; set; }
        /// <summary>
        /// 是否手动上传
        /// </summary>
        public bool ManualUpload { get; set; }
        /// <summary>
        /// 定期清理的历史数据天数
        /// </summary>
        public int DataCleanerDaysInterval { get; set; }

        public string DetectionMachineNo { get; set; }
        /// <summary>
        /// 机台类型0轮毂检测,1离线测厚
        /// </summary>
        public int MachineType { get; set; }
        /// <summary>
        /// 检测机台编号
        /// </summary>
        //public String DetectionMachineNo { get; set; }
        /// <summary>
        /// 是否使用自定义计划日期
        /// </summary>
        public bool IsUseCustomizePlanDate { get; set; }
        /// <summary>
        /// 早班时间段开始
        /// </summary>
        public string ShiftATimeStart { get; set; }
        /// <summary>
        /// 早班时间段结束
        /// </summary>
        public string ShiftATimeEnd { get; set; }
        /// <summary>
        /// 自定义计划时间
        /// </summary>
        public DateTime? CustomizePlanDate { get; set; }
        /// <summary>
        /// 胎面
        /// </summary>
        public string Folder1Path { get; set; }
        /// <summary>
        /// 胎侧
        /// </summary>
        public string Folder2Path { get; set; }
        /// <summary>
        /// 三角胶
        /// </summary>
        public string Folder3Path { get; set; }
        public string UserId { get; set; }
        public string CurrentShift { get; set; }
        public AppConfig Clone()
        {
            var json = JsonSerializeHelper.SerializeObjectToJson(this);
            var newConfig = JsonSerializeHelper.DeserializeObjectFromJson<AppConfig>(json);
            return newConfig;
        }

        public DateTime GetCurrentProcuceDate_old()
        {
            var datetime = DateTime.Now;
            try
            {
                if (this.IsUseCustomizePlanDate && this.CustomizePlanDate.HasValue)
                {
                    datetime = this.CustomizePlanDate.Value;
                }
                else
                {
                    if (TimeOnly.FromDateTime(DateTime.Now).IsBetween(TimeOnly.ParseExact(this.ShiftATimeStart, "HH:mm"), TimeOnly.ParseExact(this.ShiftATimeEnd, "HH:mm")))
                    {
                        datetime = DateTime.Now.Date;
                    }
                    else
                    {
                        datetime = DateTime.Now.Date.AddDays(-1);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return datetime;
        }
        /// <summary>
        /// 计算当前计划的生产日期。
        /// 按两班倒：白班：08:00~20:00 夜班： 20:00~08:00
        /// 主要计算的是：当天的班，如20号的班，但到了自然日21号时，本函数应返回20号而不是21号。
        /// </summary>
        /// <returns></returns>
        public DateTime GetCurrentProcuceDate()
        {
            var datetime = DateTime.Now;
            try
            {
                if (this.IsUseCustomizePlanDate && this.CustomizePlanDate.HasValue)
                {
                    datetime = this.CustomizePlanDate.Value;
                }
                else
                {
                    //24:00 要写成 00:00 否则 TimeOnly.ParseExact 会报错
                    if (TimeOnly.FromDateTime(datetime).IsBetween(TimeOnly.ParseExact("00:00", "HH:mm"), TimeOnly.ParseExact("07:00", "HH:mm")))
                    //if (TimeOnly.FromDateTime(DateTime.Now).IsBetween(TimeOnly.ParseExact(this.ShiftATimeStart, "HH:mm"), TimeOnly.ParseExact(this.ShiftATimeEnd, "HH:mm")))
                    {
                        datetime = DateTime.Now.Date.AddDays(-1);
                        return datetime;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return datetime;
        }
        /// <summary>
        /// 计算计划日。
        /// 比如说白班和夜班，7:00~19:00,19:00~7:00。
        /// 那么可以这么说：2025年3月21号是计划日，那么从7点开始到19点算21号，19点到第二天凌晨7点也算21号；此后7点就算 22号的计划日了。
        /// </summary>
        /// <returns></returns>
        public DateTime GetProcuceDate()
        {
            var datetime = DateTime.Now;
            try
            {
                //24:00 要写成 00:00 否则 TimeOnly.ParseExact 会报错
                if (TimeOnly.FromDateTime(datetime).IsBetween(TimeOnly.ParseExact("00:00", "HH:mm"), TimeOnly.ParseExact("07:00", "HH:mm")))
                //if (TimeOnly.FromDateTime(DateTime.Now).IsBetween(TimeOnly.ParseExact(this.ShiftATimeStart, "HH:mm"), TimeOnly.ParseExact(this.ShiftATimeEnd, "HH:mm")))
                {
                    datetime = DateTime.Now.Date.AddDays(-1);
                    return datetime;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return datetime;
        }

        /// <summary>
        /// 计算班次。
        /// 两班制： 白班和夜班，7:00~19:00,19:00~7:00。
        /// </summary>
        /// <returns></returns>
        internal string? GetCurrentShift()
        {
            if (TimeOnly.FromDateTime(DateTime.Now).IsBetween(TimeOnly.ParseExact(this.ShiftATimeStart, "HH:mm"),
                TimeOnly.ParseExact(this.ShiftATimeEnd, "HH:mm")))
            {
                return "早";
            }
            else
            {
                return "夜";
            }
        }
    }

    [Obsolete("还没用就废弃了")]
    public class ShiftCalculator
    {
        public enum ShiftType { DayShift, NightShift }

        public class ShiftPeriod
        {
            public DateTime StartTime { get; set; }
            public DateTime EndTime { get; set; }
            public ShiftType Shift { get; set; }
        }

        public static ShiftPeriod CalculateShifts(DateTime today)
        {
            DateTime currentDay = today;
            ShiftPeriod shift;

            // 白班 7:00-19:00 (12小时)
            DateTime dayStart = currentDay.AddHours(7);
            DateTime dayEnd = dayStart.AddHours(12);

            shift = new ShiftPeriod
            {
                StartTime = dayStart,
                EndTime = dayEnd,
                Shift = ShiftType.DayShift
            };

            // 夜班 19:00-次日7:00 (12小时)
            DateTime nightStart = dayEnd;
            DateTime nightEnd = currentDay.AddDays(1).AddHours(7);
            shift = new ShiftPeriod
            {
                StartTime = nightStart,
                EndTime = nightEnd,
                Shift = ShiftType.NightShift
            };

            currentDay = currentDay.AddDays(1);


            return shift;
        }

        public static void PrintShifts(List<ShiftPeriod> shifts)
        {
            foreach (var shift in shifts)
            {
                Console.WriteLine($"{shift.Shift}: {shift.StartTime:yyyy-MM-dd HH:mm} ~ {shift.EndTime:yyyy-MM-dd HH:mm}");
            }
        }
    }

}
